package com.qeegoo.b2bautozityp.moudle.detail.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.qeegoo.b2bautozityp.R;
import com.qeegoo.b2bautozityp.moudle.detail.bean.DetailBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by HongQian.Wang on 2017/10/18.
 */

public class LeftAdapter extends BaseAdapter {
    private int mSelectPosition = 0;
    private List<DetailBean.GoodsInfoMoreListBean> goodsInfoMoreList;

    private List<String> mSelectArrowList = new ArrayList<>();
    private TextView total_amount;

    public LeftAdapter(TextView total_amount, List<DetailBean.GoodsInfoMoreListBean> goodsInfoMoreList) {
        this.goodsInfoMoreList = goodsInfoMoreList;
        this.total_amount = total_amount;
    }

    @Override
    public int getCount() {
        return goodsInfoMoreList == null ? 0 : goodsInfoMoreList.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }


    @Override
    public View getView(int position, View view, ViewGroup parent) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.cell_left_inquery, parent, false);
        View cell_left_root = inflate.findViewById(R.id.cell_left_root);
        TextView tv_left_name = (TextView) inflate.findViewById(R.id.tv_left_name);
        ImageView iv_left_arrow = (ImageView) inflate.findViewById(R.id.iv_left_arrow);
        if (position == mSelectPosition) {
            cell_left_root.setBackgroundColor(parent.getContext().getResources().getColor(R.color.white));
            tv_left_name.setTextColor(parent.getContext().getResources().getColor(R.color.cell_left));

        } else {
            tv_left_name.setTextColor(parent.getContext().getResources().getColor(R.color.text_important));
        }

        if (mSelectArrowList.contains(String.valueOf(position))) {
            iv_left_arrow.setVisibility(View.VISIBLE);
        } else {
            iv_left_arrow.setVisibility(View.INVISIBLE);
        }
        tv_left_name.setText(goodsInfoMoreList.get(position).getProductName());

        if (total_amount != null) {
            total_amount.setText(parseList(mSelectArrowList) + "");
        }


        return inflate;
    }

    public void setCurrentPositionAndRefreshed(int currentPosition) {
        mSelectPosition = currentPosition;
        notifyDataSetChanged();
    }

    public void addSelectedArrow(int leftPosition) {
        mSelectArrowList.add(String.valueOf(leftPosition));
        notifyDataSetChanged();
    }

    public void reduceSelectedArrow(int leftPosition) {
        for (int i = 0; i < mSelectArrowList.size(); i++) {
            if (mSelectArrowList.get(i).equals(String.valueOf(leftPosition))) {
                mSelectArrowList.remove(mSelectArrowList.get(i));
                break;
            }
        }
        notifyDataSetChanged();
    }


    public int parseList(List<String> mSelectArrowList) {
        if (mSelectArrowList.size() == 0) {
            return 0;
        }
        String flag = mSelectArrowList.get(0);
        int count = 1;
        for (int i = 1; i < mSelectArrowList.size(); i++) {
            if (mSelectArrowList.get(i).equals(flag)) {
                continue;
            } else {
                count++;
                flag = mSelectArrowList.get(i);
            }
        }
        return count;
    }
}

